<template>
<view class="container" :data-festival="currentFestival.name" :class="festivalBgClass">
<!-- 顶部导航 -->
<view class="header">
<view class="header-title">节日倒计时</view>
<view class="add-btn cursor-pointer" @click="showAddDialog = true">
<uni-icons type="plus" size="24" color="#4A90E2"></uni-icons>
</view>
</view>
<!-- 中央倒计时显示区 -->
<view class="countdown-section">
<view class="festival-name">{{ currentFestival.name }}</view>
<view class="countdown-number">
  <template v-if="!isPastFestival(currentFestival)">
    {{ remainingDays }}
  </template>
  <template v-else>
    已过
  </template>
</view>
<view class="countdown-unit" v-if="!isPastFestival(currentFestival)">天</view>
<view class="countdown-date">{{ currentFestival.date }}</view>
</view>
<!-- 底部节日列表 -->
<view class="festival-list-section">
<scroll-view class="festival-scroll" scroll-x="true" show-scrollbar="false">
<view class="festival-list">
<view
v-for="(festival, index) in sortedFestivals"
:key="index"
class="festival-item cursor-pointer"
:class="{ active: currentIndex === index }"
@click="selectFestival(index)"
>
<view class="festival-icon">
<image :src="festival.icon" class="icon-image" mode="aspectFit" />
</view>
<view class="festival-name-small">{{ festival.name }}</view>
<view v-if="isPastFestival(festival)" style="font-size: 18rpx; color: #fff; background: #bbb; border-radius: 8rpx; padding: 2rpx 8rpx; margin-top: 6rpx;">已过</view>
<!-- 删除按钮：仅自定义节日显示 -->
<view v-if="festival.id && festival.bgClass" style="margin-top: 8rpx;">
<uni-icons type="close" size="18" color="#e74c3c" @click.stop="deleteFestival(festival, index)" />
</view>
</view>
</view>
</scroll-view>
</view>
<view class="privacy-policy">
<text @click="goToPrivacy" class="privacy-link cursor-pointer">隐私政策</text>
</view>
<!-- 添加节日弹窗 -->
<view v-if="showAddDialog" class="dialog-overlay" @click="closeDialog">
<view class="dialog-content" @click.stop>
<view class="dialog-header">
<view class="dialog-title">添加自定义节日</view>
<view class="close-btn cursor-pointer" @click="closeDialog">
<uni-icons type="close" size="20" color="#666666"></uni-icons>
</view>
</view>
<view class="dialog-body">
<view class="input-group">
<view class="input-label">节日名称</view>
<input
v-model="newFestival.name"
class="input-field"
placeholder="请输入节日名称"
maxlength="10"
/>
</view>
<view class="input-group">
<view class="input-label">节日日期</view>
<picker mode="date" :value="newFestival.date" @change="onDateChange">
<view class="date-picker">
{{ newFestival.date || '请选择日期' }}
</view>
</picker>
</view>
</view>
<view class="dialog-footer">
<view class="btn-cancel cursor-pointer" @click="closeDialog">取消</view>
<view class="btn-confirm cursor-pointer" @click="addFestival">确认</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
  data() {
    return {
      currentIndex: 0,
      showAddDialog: false,
      festivals: [
        {
          name: '春节',
          date: '2025-01-29',
          icon: 'https://readdy.ai/api/search-image?query=icon%20Chinese%20New%20Year%20lantern%20red%20and%20gold%20traditional%20festival%20decoration%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=festival1&orientation=squarish'
        },
        {
          name: '情人节',
          date: '2025-02-14',
          icon: 'https://readdy.ai/api/search-image?query=icon%20Valentine%20Day%20heart%20shape%20pink%20and%20red%20romantic%20love%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=festival2&orientation=squarish'
        },
        {
          name: '妇女节',
          date: '2025-03-08',
          icon: 'https://readdy.ai/api/search-image?query=icon%20International%20Women%20Day%20flower%20bouquet%20pink%20and%20purple%20elegant%20feminine%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=festival3&orientation=squarish'
        },
        {
          name: '劳动节',
          date: '2025-05-01',
          icon: 'https://readdy.ai/api/search-image?query=icon%20Labor%20Day%20work%20tools%20hammer%20and%20wrench%20blue%20and%20yellow%20industrial%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=festival4&orientation=squarish'
        },
        {
          name: '儿童节',
          date: '2025-06-01',
          icon: 'https://readdy.ai/api/search-image?query=icon%20Children%20Day%20colorful%20balloons%20and%20toys%20bright%20rainbow%20colors%20playful%20childhood%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=festival5&orientation=squarish'
        },
        {
          name: '中秋节',
          date: '2025-10-06',
          icon: 'https://readdy.ai/api/search-image?query=icon%20Mid%20Autumn%20Festival%20full%20moon%20and%20mooncake%20golden%20yellow%20traditional%20Chinese%20festival%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=festival6&orientation=squarish'
        },
        {
          name: '国庆节',
          date: '2025-10-01',
          icon: 'https://readdy.ai/api/search-image?query=icon%20National%20Day%20Chinese%20flag%20red%20and%20yellow%20patriotic%20celebration%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=festival7&orientation=squarish'
        },
        {
          name: '圣诞节',
          date: '2025-12-25',
          icon: 'https://readdy.ai/api/search-image?query=icon%20Christmas%20tree%20green%20and%20red%20festive%20holiday%20decoration%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=festival8&orientation=squarish'
        }
      ],
      customFestivals: [], // 新增：自定义节日数组
      newFestival: {
        name: '',
        date: '',
        icon: 'https://readdy.ai/api/search-image?query=icon%20custom%20festival%20celebration%20star%20and%20sparkles%20golden%20yellow%20festive%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=custom1&orientation=squarish'
      }
    }
  },
  computed: {
    currentFestival() {
      return this.sortedFestivals[this.currentIndex] || { name: '', date: '', icon: '', id: '', bgClass: '' }
    },
    remainingDays() {
      // 获取今天的日期（去除时分秒）
      const today = new Date();
      today.setHours(0, 0, 0, 0);
      // 获取目标日期（去除时分秒）
      const targetDate = new Date(this.currentFestival.date);
      targetDate.setHours(0, 0, 0, 0);
      // 计算天数差
      const timeDiff = targetDate.getTime() - today.getTime();
      const daysDiff = Math.round(timeDiff / (1000 * 3600 * 24));
      return daysDiff > 0 ? daysDiff : 0;
    },
    sortedFestivals() {
      // 合并自定义节日和预设节日
      const today = new Date();
      today.setHours(0, 0, 0, 0);
      const all = this.festivals.concat(this.customFestivals);
      return all.slice().sort((a, b) => {
        const aDate = new Date(a.date);
        aDate.setHours(0, 0, 0, 0);
        const bDate = new Date(b.date);
        bDate.setHours(0, 0, 0, 0);
        const aPast = aDate < today;
        const bPast = bDate < today;
        if (aPast === bPast) {
          return aDate - bDate;
        }
        return aPast ? 1 : -1;
      });
    },
    festivalBgClass() {
      // 预设节日名和 class 的映射
      const preset = {
        '春节': '',
        '情人节': '',
        '妇女节': '',
        '劳动节': '',
        '儿童节': '',
        '中秋节': '',
        '国庆节': '',
        '圣诞节': ''
      }
      // 如果是自定义节日，返回其 bgClass
      if (this.currentFestival.bgClass) {
        return this.currentFestival.bgClass
      }
      // 预设节日不加 class，样式用 data-festival
      return ''
    }
  },
  methods: {
    isPastFestival(festival) {
      const today = new Date();
      today.setHours(0, 0, 0, 0);
      const festivalDate = new Date(festival.date);
      festivalDate.setHours(0, 0, 0, 0);
      return festivalDate < today;
    },
    selectFestival(index) {
      this.currentIndex = index
    },
    onDateChange(e) {
      this.newFestival.date = e.detail.value
    },
    addFestival() {
      if (this.newFestival.name && this.newFestival.date) {
        // 分配唯一 id 和循环分配 bgClass
        const id = 'custom_' + Date.now() + '_' + Math.floor(Math.random() * 10000);
        const bgClass = 'custom-bg-' + ((this.customFestivals.length % 3) + 1);
        const newFest = {
          name: this.newFestival.name,
          date: this.newFestival.date,
          icon: this.newFestival.icon,
          id,
          bgClass
        };
        this.customFestivals.push(newFest);
        // 缓存到本地
        uni.setStorageSync('customFestivals', this.customFestivals);
        this.newFestival = {
          name: '',
          date: '',
          icon: 'https://readdy.ai/api/search-image?query=icon%20custom%20festival%20celebration%20star%20and%20sparkles%20golden%20yellow%20festive%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=custom2&orientation=squarish'
        }
        this.showAddDialog = false
        uni.showToast({
          title: '添加成功',
          icon: 'success'
        })
      } else {
        uni.showToast({
          title: '请填写完整信息',
          icon: 'none'
        })
      }
    },
    deleteFestival(festival, index) {
      // 只允许删除自定义节日
      const idx = this.customFestivals.findIndex(f => f.id === festival.id);
      if (idx !== -1) {
        this.customFestivals.splice(idx, 1);
        uni.setStorageSync('customFestivals', this.customFestivals);
        // 如果当前选中被删，重置 currentIndex
        if (this.sortedFestivals[this.currentIndex]?.id === festival.id) {
          this.currentIndex = 0;
        }
        uni.showToast({ title: '已删除', icon: 'success' });
      }
    },
    closeDialog() {
      this.showAddDialog = false
      this.newFestival = {
        name: '',
        date: '',
        icon: 'https://readdy.ai/api/search-image?query=icon%20custom%20festival%20celebration%20star%20and%20sparkles%20golden%20yellow%20festive%20symbol%20prominent%20main%20subject%20subject%20fills%2080%20percent%20of%20frame%20isolated%20on%20white%20background%20centered%20composition%20soft%20lighting%20no%20shadows%20clean%20minimalist%20design&width=80&height=80&seq=custom3&orientation=squarish'
      }
    },
    goToPrivacy() {
      uni.navigateTo({
        url: '/pages/index3'
      })
    }
  },
  mounted() {
    // 页面加载完成，读取本地自定义节日
    const local = uni.getStorageSync('customFestivals') || [];
    if (Array.isArray(local)) {
      this.customFestivals = local;
    }
  }
}
</script>
<style>
page {
height: 100%;
}
.container {
height: 100%;
display: flex;
flex-direction: column;
transition: background 0.5s ease;
}
.container[data-festival="春节"] {
background: linear-gradient(180deg, #FFE4E4 0%, #FFF1E8 100%);
}
.container[data-festival="情人节"] {
background: linear-gradient(180deg, #FFE8F4 0%, #FFF0F5 100%);
}
.container[data-festival="妇女节"] {
background: linear-gradient(180deg, #F8E8FF 0%, #FFE8F4 100%);
}
.container[data-festival="劳动节"] {
background: linear-gradient(180deg, #E8F6FF 0%, #F0F8FF 100%);
}
.container[data-festival="儿童节"] {
background: linear-gradient(180deg, #E8FFF4 0%, #F0FFFA 100%);
}
.container[data-festival="中秋节"] {
background: linear-gradient(180deg, #FFF6E8 0%, #FFF9F0 100%);
}
.container[data-festival="国庆节"] {
background: linear-gradient(180deg, #FFE8E8 0%, #FFF0F0 100%);
}
.container[data-festival="圣诞节"] {
background: linear-gradient(180deg, #FFE8E8 0%, #F0FFF2 100%);
}
/* 自定义节日背景色 */
.custom-bg-1 {
  background: linear-gradient(180deg, #FFFDE4 0%, #F9F6FF 100%);
}
.custom-bg-2 {
  background: linear-gradient(180deg, #E4FFF7 0%, #E8F7FF 100%);
}
.custom-bg-3 {
  background: linear-gradient(180deg, #FFF9E4 0%, #FFF6E8 100%);
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 60rpx 40rpx 20rpx;
flex-shrink: 0;
}
.header-title {
font-size: 36rpx;
font-weight: 600;
color: #333333;
}
.add-btn {
width: 48rpx;
height: 48rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 24rpx;
background: rgba(255, 255, 255, 0.8);
box-shadow: 0 4rpx 12rpx rgba(74, 144, 226, 0.15);
}
.countdown-section {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 0 40rpx;
}
.festival-name {
font-size: 32rpx;
color: #666666;
margin-bottom: 40rpx;
font-weight: 500;
}
.countdown-number {
font-size: 180rpx;
font-weight: 700;
color: #333333;
line-height: 1;
margin-bottom: 20rpx;
text-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
}
.countdown-unit {
font-size: 48rpx;
color: #666666;
margin-bottom: 30rpx;
font-weight: 500;
}
.countdown-date {
font-size: 28rpx;
color: #999999;
background: rgba(255, 255, 255, 0.6);
padding: 16rpx 32rpx;
border-radius: 20rpx;
}
.festival-list-section {
flex-shrink: 0;
padding: 40rpx 0 60rpx;
background: rgba(255, 255, 255, 0.3);
backdrop-filter: blur(10rpx);
}
.festival-scroll {
width: 100%;
overflow: auto;
}
.festival-list {
display: flex;
padding: 0 40rpx;
gap: 32rpx;
}
.festival-item {
display: flex;
flex-direction: column;
align-items: center;
min-width: 120rpx;
padding: 20rpx;
border-radius: 20rpx;
transition: all 0.3s ease;
}
.festival-item.active {
background: rgba(74, 144, 226, 0.1);
box-shadow: 0 8rpx 20rpx rgba(74, 144, 226, 0.2);
}
.festival-icon {
width: 80rpx;
height: 80rpx;
border-radius: 40rpx;
background: rgba(255, 255, 255, 0.8);
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 16rpx;
box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}
.icon-image {
width: 60rpx;
height: 60rpx;
border-radius: 30rpx;
}
.festival-name-small {
font-size: 24rpx;
color: #666666;
text-align: center;
font-weight: 500;
}
.festival-item.active .festival-name-small {
color: #4A90E2;
font-weight: 600;
}
.dialog-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
padding: 40rpx;
}
.dialog-content {
background: #ffffff;
border-radius: 24rpx;
width: 100%;
max-width: 600rpx;
box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.15);
}
.dialog-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 40rpx 40rpx 20rpx;
border-bottom: 2rpx solid #f5f5f5;
}
.dialog-title {
font-size: 32rpx;
font-weight: 600;
color: #333333;
}
.close-btn {
width: 40rpx;
height: 40rpx;
display: flex;
align-items: center;
justify-content: center;
}
.dialog-body {
padding: 40rpx;
}
.input-group {
margin-bottom: 40rpx;
}
.input-label {
font-size: 28rpx;
color: #666666;
margin-bottom: 16rpx;
font-weight: 500;
}
.input-field {
height: 88rpx;
border: 2rpx solid #e5e5e5;
border-radius: 12rpx;
padding: 0 24rpx;
font-size: 28rpx;
color: #333333;
background: #fafafa;
}
.input-field:focus {
border-color: #4A90E2;
background: #ffffff;
}
.date-picker {
height: 88rpx;
border: 2rpx solid #e5e5e5;
border-radius: 12rpx;
padding: 0 24rpx;
font-size: 28rpx;
color: #333333;
background: #fafafa;
display: flex;
align-items: center;
}
.dialog-footer {
display: flex;
gap: 24rpx;
padding: 20rpx 40rpx 40rpx;
}
.btn-cancel,
.btn-confirm {
flex: 1;
height: 88rpx;
border-radius: 12rpx;
display: flex;
align-items: center;
justify-content: center;
font-size: 28rpx;
font-weight: 500;
}
.btn-cancel {
background: #f5f5f5;
color: #666666;
}
.btn-confirm {
background: #4A90E2;
color: #ffffff;
}
.cursor-pointer {
cursor: pointer;
}
.privacy-policy {
text-align: center;
padding: 20rpx 0;
font-size: 24rpx;
color: #999999;
background: rgba(255, 255, 255, 0.3);
}
.privacy-link {
text-decoration: underline;
}
</style>